from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
import pandas as pd

driver = webdriver.Chrome(service=Service('D:\\myproject\\feng-huang\\1.自动化\\1.selenium\\chromedriver.exe'))

driver.maximize_window()
data = []
for i in range(458, 485):
    driver.get(f'https://www.foodtalks.cn/company/select?page={i}')

    time.sleep(5)
    names = driver.find_elements(By.XPATH,
                                 '/html/body/div[1]/div[2]/section/main/section/main/div[1]/div[2]/div[2]/div')
    for name in names:
        dict = {'简称': '', '产品类别': '', '排名': '', '位置': '', '产品': '', 'url': '',
                }
        try:
            title = name.find_element(By.XPATH, './a/div[2]/h2')
            title_text = title.text
        except:
            title_text = ''
        dict['简称'] = title_text
        try:
            Product_Category = name.find_element(By.XPATH, './a/div[2]/div[1]').text
        except:
            Product_Category = ''
        dict['产品类别'] = Product_Category
        try:
            Rankings = name.find_element(By.CLASS_NAME, 'rankings').text
        except:
            Rankings = ''
        dict['排名'] = Rankings
        try:
            Location = name.find_element(By.CLASS_NAME, 'location').text
        except:
            Location = ''
        dict['位置'] = Location

        try:
            Product = name.find_element(By.CLASS_NAME, 'products').text
        except:
            Product = ''
        dict['产品'] = Product
        try:
            url = name.find_element(By.XPATH, './a')
            dict['url'] = url.get_attribute('href')
        except:
            dict['url'] = ''
        data.append(dict)
        name.find_element(By.XPATH, './a/div[2]/h2').click()
        time.sleep(2)
        driver.switch_to.window(driver.window_handles[1])
        try:
            driver.find_element(By.XPATH, '//span[text()="首页"]').click()
        except:
            pass
        # 中文全称
        try:
            company_name = driver.find_element(By.XPATH, '//div[text()="中文全称："]')
            if company_name.text == '中文全称：':
                try:
                    company_name1 = company_name.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_name1 = ''
                dict['公司名称'] = company_name1
        except:
            company_name = ''
        # 中文简称
        try:
            company_name_short = driver.find_element(By.XPATH, '//div[text()="中文简称："]')
            if company_name_short.text == '中文简称：':
                try:
                    company_name_short1 = company_name_short.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_name_short1 = ''
                dict['公司简称'] = company_name_short1
        except:
            dict['公司简称'] = ''
        # 英文全称
        try:
            company_name_en = driver.find_element(By.XPATH, '//div[text()="英文全称："]')
            if company_name_en.text == '英文全称：':
                try:
                    company_name_en1 = company_name_en.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_name_en1 = ''
                dict['英文名称'] = company_name_en1
            # 英文简称
            company_name_short_en = driver.find_element(By.XPATH, '//div[text()="英文简称："]')
            if company_name_short_en.text == '英文简称：':
                try:
                    company_name_short_en1 = company_name_short_en.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_name_short_en1 = ''
                dict['英文简称'] = company_name_short_en1
        except:
            dict['英文名称'] = ''
        # 公司类型
        try:
            company_type = driver.find_element(By.XPATH, '//div[text()="公司类型："]')
            if company_type.text == '公司类型：':
                try:
                    company_type1 = company_type.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_type1 = ''
                dict['公司类型'] = company_type1
            # 成立时间
            establish_time = driver.find_element(By.XPATH, '//div[text()="成立时间："]')
            if establish_time.text == '成立时间：':
                try:
                    establish_time1 = establish_time.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    establish_time1 = ''
                dict['成立时间'] = establish_time1
        except:
            dict['公司类型'] = ''
        # 股票代码
        try:
            stock_code = driver.find_element(By.XPATH, '//div[text()="股票代码："]')
            if stock_code.text == '股票代码：':
                try:
                    stock_code1 = stock_code.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    stock_code1 = ''
                dict['股票代码'] = stock_code1
            # 公司总机
            company_tel = driver.find_element(By.XPATH, '//div[text()="公司总机："]')
            if company_tel.text == '公司总机：':
                try:
                    company_tel1 = company_tel.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_tel1 = ''
                dict['公司总机'] = company_tel1
        except:
            dict['股票代码'] = ''
        # 公司性质
        try:
            company_property = driver.find_element(By.XPATH, '//div[text()="公司性质："]')
            if company_property.text == '公司性质：':
                try:
                    company_property1 = company_property.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_property1 = ''
                dict['公司性质'] = company_property1
        except:
            dict['公司性质'] = ''
        # 行业分类
        try:
            industry_classification = driver.find_element(By.XPATH, '//div[text()="行业分类："]')
            if industry_classification.text == '行业分类：':
                try:
                    industry_classification1 = industry_classification.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    industry_classification1 = ''
                dict['行业分类'] = industry_classification1
        except:
            dict['行业分类'] = ''
        # 主要品牌
        try:
            main_brand = driver.find_element(By.XPATH, '//div[text()="主要品牌："]')
            if main_brand.text == '主要品牌：':
                try:
                    main_brand1 = main_brand.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    main_brand1 = ''
                dict['主要品牌'] = main_brand1
        except:
            dict['主要品牌'] = ''
        # 主要产品
        try:
            main_product = driver.find_element(By.XPATH, '//div[text()="主要产品："]')
            if main_product.text == '主要产品：':
                try:
                    main_product1 = main_product.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    main_product1 = ''
                dict['主要产品'] = main_product1
        except:
            dict['主要产品'] = ''
        # 公司官网
        try:
            company_website = driver.find_element(By.XPATH, '//div[text()="公司官网："]')
            if company_website.text == '公司官网：':
                try:
                    company_website1 = company_website.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_website1 = ''
                dict['公司官网'] = company_website1
        except:
            dict['公司官网'] = ''
        # 公司总部
        try:
            company_headquarters = driver.find_element(By.XPATH, '//div[text()="公司总部："]')
            if company_headquarters.text == '公司总部：':
                try:
                    company_headquarters1 = company_headquarters.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    company_headquarters1 = ''
                dict['公司总部'] = company_headquarters1
        except:
            dict['公司总部'] = ''
        # 总部地址
        try:
            headquarters_address = driver.find_element(By.XPATH, '//div[text()="总部地址："]')
            if headquarters_address.text == '总部地址：':
                try:
                    headquarters_address1 = headquarters_address.find_element(By.XPATH, "./following-sibling::*[1]").text
                except:
                    headquarters_address1 = ''
                dict['总部地址'] = headquarters_address1
        except:
            dict['总部地址'] = ''
        # 简介
        try:
            unfold_more = driver.find_element(By.XPATH, '//span[text()="展开"]').click()
        except:
            pass
        try:
            introduction = driver.find_element(By.XPATH, '//span[text()="公司介绍"]')
            if introduction.text == '公司介绍':
                try:
                    introduction1 = introduction.find_element(By.XPATH,
                                                              '//*[@id="app"]/div[2]/section/main/section/main/div[1]/div[1]/div[4]/div/div[1]/div[1]/div[2]/div[1]').text
                except:
                    introduction1 = ''
                dict['公司介绍'] = introduction1
        except:
            dict['公司介绍'] = ''
        # 品牌
        try:
            brand = driver.find_element(By.XPATH, '//span[text()="品牌"]')
            if brand.text == '品牌':
                brand.click()
                time.sleep(1)
                list_brand = []
                try:
                    brands1 = driver.find_elements(By.CLASS_NAME, 'el-row')
                    for brand11 in brands1:
                        list_brand.append(brand11.text)
                except:
                    list_brand = ''
                dict['品牌'] = list_brand
        except:
            dict['品牌'] = ''
        # 产品
        try:
            product2 = driver.find_element(By.XPATH, '//span[text()="产品"]')
            if product2.text == '产品':
                product2.click()
                time.sleep(1)
                list_prod = []
                try:
                    prods1 = driver.find_elements(By.CLASS_NAME, 'el-row')
                    for prod11 in prods1:
                        list_prod.append(prod11.text)
                except:
                    list_prod = ''
                dict['产品'] = list_prod
        except:
            dict['产品'] = ''
        # 客户
        try:
            customer = driver.find_element(By.XPATH, '//span[text()="客户"]')
            if customer.text == '客户':
                customer.click()
                time.sleep(1)
                list_cust = []
                try:
                    custs1 = driver.find_elements(By.CLASS_NAME, 'customer-list.not-login')
                    for cust11 in custs1:
                        list_cust.append(cust11.text)
                except:
                    list_cust = ''
                dict['客户'] = list_cust
        except:
            dict['客户'] = ''
        # 供应商
        try:
            supplier = driver.find_element(By.XPATH, '//span[text()="供应商"]')
            if supplier.text == '供应商':
                supplier.click()
                time.sleep(1)
                list_supp = []
                try:
                    supp1 = driver.find_elements(By.CLASS_NAME, 'customer-list.not-login')
                    for supp11 in supp1:
                        list_supp.append(supp11.text)
                except:
                    list_supp = ''
                dict['供应商'] = list_supp
        except:
            dict['供应商'] = ''
        # 联系方式
        try:
            contact = driver.find_element(By.XPATH, '//span[text()="联系方式"]')
            if contact.text == '联系方式':
                contact.click()
                time.sleep(1)
                try:
                    contact1 = driver.find_element(By.ID, 'section-content').text
                except:
                    contact1 = ''
                dict['联系方式'] = contact1
        except:
            dict['联系方式'] = ''
        driver.close()
        driver.switch_to.window(driver.window_handles[0])
        time.sleep(2)
    data = pd.DataFrame(data)
    data.to_excel(f'./data/第{i}页数据.xlsx', index=False, engine='openpyxl')
    print(f'第{i}页数据获取完成')
    data = []
    # print(data)

# 如果你想将数据保存到Excel文件中，可以取消下面两行的注释


time.sleep(20)